package com.zhsource.tourist.controller;


import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.zhsource.article.service.ICommentService;
import com.zhsource.exception.GlobalException;
import com.zhsource.utils.R;
import com.zhsource.article.document.Comment;

import java.util.List;

/**
 * <p>@ClassName：CommentController
 * <p>@Date 2023/10/17  10:21
 *
 * @Author zzl
 **/
@Slf4j
@Api(tags = "文章评论模块控制器")
@RestController
@RequestMapping("/tourist/comment")
public class CommentController {

    @Autowired
    private ICommentService commentService;

    // 查询所有

    @ApiOperation("查询所有")
    @GetMapping("/{articleId}")
    public R<List<Comment>> findByArticleId(@PathVariable("articleId") Long articleId) {
        try {
            return R.ok("查询所有成功!", commentService.findByArticleId(articleId));
        } catch (GlobalException e) {
            log.error("Comment模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Comment模块的查询所有发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }

    // 新增
    @PostMapping
    public R<Void> insert(@RequestBody Comment comment) {
        try {
            commentService.insert(comment);
            return R.ok("新增数据成功!");
        } catch (GlobalException e) {
            log.error("Comment模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Comment模块的新增发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }
    // 删除单个

    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable("id") String id) {
        try {
            commentService.delete(id);
            return R.ok("删除单个成功!");
        } catch (GlobalException e) {
            log.error("Comment模块的删除单个发生异常,异常信息为:{}", e.getMessage());
            return R.fail(e.getMessage());
        } catch (Exception e) {
            log.error("Comment模块的查询删除发生异常,异常信息为:{}", e.getMessage());
            return R.fail("系统异常,请重试");
        }
    }


}
